/*
 * usbcomm.h
 *
 *  Created on: Mar 9, 2012
 *      Author: Victor Nidens victornlj@gmail.com
 *
 *	This file is part of API LEGO for CMLab based on LEGO NXT Direct Commands API
 *	by Michael Collins michaelcollins@ivorycity.com and
 *	Aran Deltac aran@arandeltac.com
 *
 *	API LEGO for CMLab is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Lesser General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	API LEGO for CMLab library is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Lesser General Public License for more details.
 *
 *	You should have received a copy of the GNU Lesser General Public License
 *	along with API LEGO for CMLab. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef USBCOMM_H_
#define USBCOMM_H_

#include "nxtconst.h"
#include "libusb-1.0/libusb.h"

/**
 *
 *	Presents a USB interface to the LEGO NXT.
 *
 */

#define USB_ID_VENDOR_LEGO 0x0694
#define USB_ID_PRODUCT_NXT 0x0002
#define USB_OUT_ENDPOINT   0x01
#define USB_IN_ENDPOINT    0x82
#define USB_TIMEOUT        1000
#define USB_READSIZE       64
#define USB_INTERFACE      0

/**
 * Initializes libusb library
 *
 * @return
 *
 */
int initUSB();

/**
 * Connects to the first founded NXT device
 */
int connectNXTDevice(libusb_device_handle **deviceHandle);

/**
* Connect to the specified number of devices.
*
* @param deviceHandle			Array of the device handles.
								After return contains handles of connected devices.
* @param devicesToConnect		Length of the device handles array.
* 								After return contains number of connected devices.
*/
int connectNXTDevices(libusb_device_handle **deviceHandle, uint8 *devicesToConnect);

/**
 * Sends data to the NXT device and receives response if needed
 *
 * @param deviceHandle
 * @param requestBuffer			Data buffer to transmit to the NXT
 * @param requestLength			Length of the requestBuffer
 * @param responseRequired		Flag if response from the device is required
 * @param responseBuffer		Buffer to store response from the device
 * @param responseLength		Max length of the responseBuffer. After receiving response from the NXT contains length of the received data.
 *
 * @return
 *
 */
int executeCommand(libusb_device_handle **deviceHandle, uint8 *requestBuffer, uint8 requestLength, uint8 responseRequired, uint8 *responseBuffer, int *responseLength);

/**
 * Close device I/O channels and deinitialize libusb library
 *
 * @param deviceHandle
 *
 */
void freeUSB(libusb_device_handle **deviceHandle);


#endif /* USBCOMM_H_ */
